package com.zhiqianbao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.CouponInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2017/7/18.
 */
public class CouponAdapter extends BaseAdapter{
    List<CouponInfo.DataBean.CouponListBean> couponInfoList;
    Context mContext;
    public CouponAdapter(Context context){
        mContext=context;
        couponInfoList=new ArrayList<>();
    }
    public void addAllData(List<CouponInfo.DataBean.CouponListBean> allItems){
        couponInfoList.addAll(allItems);
    }

    public void addData(CouponInfo.DataBean.CouponListBean item){
        couponInfoList.add(item);
    }

    public void clearData(){
        couponInfoList.clear();
    }
    @Override
    public int getCount() {
        return couponInfoList.size();
    }

    @Override
    public CouponInfo.DataBean.CouponListBean getItem(int position) {
        return couponInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView= LayoutInflater.from(mContext)
                    .inflate(R.layout.adapter_mycoupon_layout,null);
            viewHolder.coupon_name=(TextView)convertView
                    .findViewById(R.id.coupon_name);
            viewHolder.brief_introduction=(TextView)convertView
                    .findViewById(R.id.brief_introduction);
            viewHolder.coupon_used_state=(ImageView)convertView
                    .findViewById(R.id.coupon_used_state);
            viewHolder.repay_head=(ImageView)convertView
                    .findViewById(R.id.repay_head);
            viewHolder.coupon_effective_date=(TextView)convertView
                    .findViewById(R.id.coupon_effective_date);
            viewHolder.coupon_detail_layout=(RelativeLayout)convertView
                    .findViewById(R.id.coupon_detail_layout);
            viewHolder.img_detail=(ImageView)convertView
                    .findViewById(R.id.img_detail);
            viewHolder.show_detail_tv=(TextView)convertView
                    .findViewById(R.id.show_detail_tv);
            viewHolder.show_detail_layout=(LinearLayout) convertView
                    .findViewById(R.id.show_detail_layout);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        CouponInfo.DataBean.CouponListBean listBean=couponInfoList.get(position);
        viewHolder.brief_introduction.setText(listBean.getNote());
        viewHolder.coupon_name.setText(listBean.getName());
        viewHolder.coupon_effective_date.setText(couponInfoList
                .get(position).getEndTimeStr());
        if(listBean.getStatus().equals("1")){
            viewHolder.coupon_used_state.setVisibility(View.GONE);
            viewHolder.repay_head.setBackgroundResource(R.drawable.coupon_using_head);
            viewHolder.coupon_effective_date.setText("有效期至："+couponInfoList
                    .get(position).getEndTime());
        }
        else if(listBean.getStatus().equals("2")){
            viewHolder.repay_head.setBackgroundResource(R.drawable.coupon_used_head);
            viewHolder.coupon_used_state.setVisibility(View.VISIBLE);
            viewHolder.coupon_used_state.setImageResource(R.drawable.coupon_state_used);
            viewHolder.coupon_effective_date.setText("有效期至："+couponInfoList
                    .get(position).getEndTime());
        }
        else{
            viewHolder.repay_head.setBackgroundResource(R.drawable.coupon_used_head);
            viewHolder.coupon_used_state.setVisibility(View.VISIBLE);
            viewHolder.coupon_used_state.setImageResource(R.drawable.coupon_state_overdue);
            viewHolder.coupon_effective_date.setText("有效期至："+couponInfoList
                    .get(position).getEndTime());
        }
        viewHolder.show_detail_tv.setText(listBean.getDescreption());

        if(listBean.getUpDownState()==1){
            viewHolder.show_detail_layout.setVisibility(View.VISIBLE);
            viewHolder.img_detail.setImageResource(R.drawable.coupon_up);
        }
        else{
            viewHolder.show_detail_layout.setVisibility(View.GONE);
            viewHolder.img_detail.setImageResource(R.drawable.coupon_down);
        }
        viewHolder.coupon_detail_layout.setTag(listBean);
        viewHolder.coupon_detail_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CouponInfo.DataBean.CouponListBean item= (CouponInfo.DataBean
                        .CouponListBean) v.getTag();
                if(item.getUpDownState()==1){
                    item.setUpDownState(2);
                }
                else{
                    item.setUpDownState(1);
                }
                notifyDataSetChanged();
            }

        });
        return convertView;
    }
    public class ViewHolder{
        TextView brief_introduction;
        TextView coupon_name;
        ImageView coupon_used_state;
        ImageView repay_head;
        TextView coupon_effective_date;
        RelativeLayout coupon_detail_layout;
        ImageView img_detail;
        TextView show_detail_tv;
        LinearLayout show_detail_layout;
    }
}
